<?php
/**
 * Class Xu ly thong thong tin loai danh muc
 */
class Ajax_IndexController extends  Zend_Controller_Action {
	public $baseUrl;
	public function init(){
		Zend_Loader::loadClass('Zend_Json');	    
	}	
	/**
	 * Creater : Truongdv
	 * Date : 18/10/2012
	 * Idea : Tao phuong thuc hien lay du lieu trong danh muc
	 */
	public function indexAction(){
		//Tao doi tuong
		$objFunction = new Efy_Function_RecordFunctions();
		$typelist 	= $this->_request->getParam('typelist');
		//Lay thong tin
		$arrResult = Efy_Function_RecordFunctions::loadCache($typelist);		
		echo Zend_Json::encode($arrResult);
		die();		
	}
	public function uploadfileAction(){
		$objFunction = new Efy_Function_RecordFunctions();
		$objconfig = new Efy_Init_Config();

		$uploaddir=$objconfig->_setAttachFileUrlPath().'tempupload/';
		$result = $objFunction->uploadAjaxTemp($uploaddir);
		echo $result;
		$this->getHelper('viewRenderer')->setNoRender();  
	}
	public function uploadthumbnailAction(){
		$objFunction = new Efy_Function_RecordFunctions();
		$objconfig = new Efy_Init_Config();
		$uploaddir=$objconfig->_setAttachFileUrlPath().'tempupload/';
		$result = $objFunction->uploadAjaxTempImage($uploaddir);
		echo $result;
		$this->getHelper('viewRenderer')->setNoRender();  
	}
	
	/**
	* Creater: Truongdv
	* Date: 30/08/2012
	* Idea: Lay phong ban theo don vi
	*/ 
	public function getunitbypermissionAction(){
		$ownerCode = $this->_request->getParam('value');
		$arrUnit = $_SESSION['UNIT_PRIVATE'];
		$fk_unit ='';
		$arrR04 = array();
		foreach ($arrUnit as $key => $value) {
			if($ownerCode == $value['C_CODE']){
				$fk_unit = $value['PK_UNIT'];
				break;
			}
		}
		$arrDepartment = $_SESSION['DEPARTMENT_PRIVATE'];
		//Lay phong ban
		foreach ($arrDepartment as $key => $value) {
			if($fk_unit == $value['FK_UNIT']){
				array_push($arrR04, array('PK_OBJECT'=>$value['PK_OBJECT']
											,'C_NAME'=>$value['C_NAME']
											,'C_CODE'=>$value['C_CODE']
											));
			}
		}
		echo Zend_Json::encode($arrR04);
		$this->getHelper('viewRenderer')->setNoRender();  
	}	
	/**
	* Creater: Phuongtt
	* Date: 12/01/2013
	* Idea: Xóa tài liệu kèm theo(Xóa file gốc và xóa trong db)
	*/ 
	public function deletefileAction(){		
		$objFunction = new Efy_Function_RecordFunctions();		
		$objconfig = new Efy_Init_Config();
		$recordId = $this->_request->getParam('recordId');
		$listFile = $this->_request->getParam('listFile');
		$type = $this->_request->getParam('type');
		$arrListUrl = explode('!~@~!', $listFile);		
		$fileNameList = '';
		$sDelimitor ='~!@#';
		foreach ($arrListUrl as $key => $value) {
			$arr = explode('/', $value);
			$fileNameList .= end($arr).$sDelimitor;
			$file = '..'.$value;
			if(file_exists($file)){				
				unlink($file);
			}				
		}
		$fileTableInfo = $objconfig->_getAttachFileInfo($type);
		$fileNameList = substr($fileNameList, 0,strlen($fileNameList)- strlen($sDelimitor));				
		if($recordId != ''){
			$objFunction->DelFileInDB($fileTableInfo['TABLE_NAME'], $fileTableInfo['COLUMN_ID'], $recordId, $fileTableInfo['DOC_TYPE'], $fileNameList);
		}
		$imageAvatar = $objconfig->_setImageUrlPath().'avatar_default.png';
		echo $imageAvatar;
		$this->getHelper('viewRenderer')->setNoRender();  	
	}
	public function loadtreeAction(){		
		Zend_Loader::loadClass('Efy_DB_Connection');
		$objDb	= new Efy_DB_Connection();
		$arrTree = '';
		$sType = $this->_request->getParam('sType',null);
		$searchNodes = $this->_request->getParam('searchNodes',null);
		$viewPer = $this->_request->getParam('viewPer',null);  //1 : neu chi cho phep can bo co quyen thuc hien xem cay,0: moi can 
		// bo deu duoc nhin thay cay
		$staff_id = '';
		if($viewPer == 1)
			$staff_id = $_SESSION['staff_id'];
		else $staff_id = '';
		$ownerCode = $_SESSION['OWNER_CODE'];
		if ($ownerCode == 'NQ')
			$ownerCode = '';
		$arrParameter = array(
			'status'=>'HOAT_DONG'
			,'txtFullTextSearch'=>''
			,'type'=>$sType
			,'sOwner_code'=>$ownerCode
			,'staff_id'=> $staff_id
		);	
		if ($sType == 'TRAINING'){
			// if(!isset($_SESSION['TREE_TRAINING'])){				
				$_SESSION['TREE_TRAINING'] = $objDb->mTLSPRun('mTL_TreeGetAll',$arrParameter,1,0);								
			// }
			$arrTreeGetAll = $_SESSION['TREE_TRAINING'];
		}
		else if ($sType == 'LEARNING'){
			// if(!isset($_SESSION['TREE_LEARNING'])){				
				$_SESSION['TREE_LEARNING'] = $objDb->mTLSPRun('mTL_TreeGetAll',$arrParameter,1,0);								
			// }
			$arrTreeGetAll = $_SESSION['TREE_LEARNING'];
		}
		$idNode = $this->_request->getParam('id',null);
		$arrTree = $this->getChildNode($idNode,$arrTreeGetAll);
		if ($searchNodes != '')
			$arrTree = $arrTreeGetAll;
		echo json_encode($arrTree);
		$this->getHelper('viewRenderer')->setNoRender();  
	}
	public function getChildNode($idNode,$arrTreeGetAll){
		$arrOutput = array();
		foreach ($arrTreeGetAll as $key => $value) {
			if($value['FK_OBJECT'] == $idNode){
				array_push($arrOutput, array('attr'=>array(
												'id'=>$value['PK_OBJECT']
												,'rel'=>'folder'
												,'per'=>$value['C_PER']
											),
											'data'=>$value['TR04'],
											'state'=>'closed'
				));
			}
		}
		return $arrOutput;
	}
}
?>